package com.skydonkey.dao;

import com.github.pagehelper.Page;
import com.skydonkey.pojo.po.AccountFollows;
import com.skydonkey.pojo.po.AccountInfoDetails;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AccountFollowsMapper {

    @Insert("INSERT INTO account_follows (uid, follow_uid) " +
            "VALUES (#{uid}, #{followUid})")
    @Options(useGeneratedKeys = true, keyProperty = "recordId")
    int addFollow(AccountFollows accountFollows);

    @Delete("DELETE FROM account_follows " +
            "WHERE uid = #{uid} AND follow_uid = #{followUid}")
    int removeFollow(@Param("uid") Long uid, @Param("followUid") Long followUid);

    @Select("SELECT follow_uid FROM account_follows " +
            "WHERE uid = #{uid}")
    Page<Long> getFollowingUsers(@Param("uid") Long uid);

    @Select("SELECT uid FROM account_follows " +
            "WHERE follow_uid = #{followUid}")
    Page<Long> getFollowers(@Param("followUid") Long followUid);

    @Select("SELECT COUNT(*) FROM account_follows " +
            "WHERE follow_uid = #{followUid}")
    int getFollowerCount(@Param("followUid") Long followUid);

    @Select("SELECT COUNT(*) FROM account_follows " +
            "WHERE uid = #{uid}")
    int getFollowingCount(@Param("uid") Long uid);

    @Select("SELECT COUNT(1) from account_follows where uid = #{uid} and follow_uid = #{followUid}")
    int isFollowed(Long uid, Long followUid);

    @Select("SELECT " +
            "a.uid AS uid, " +
            "ai.head_pic_url AS headPicUrl, " +
            "ai.birthday AS birthday, " +
            "ai.brief_introduction AS briefIntroduction, " +
            "ai.signature AS signature, " +
            "ai.sex AS sex, " +
            "ai.province AS province, " +
            "ai.county AS county, " +
            "a.nickname AS nickname, " +
            "COUNT(f.follow_uid) AS followsCount, " +
            "EXISTS (SELECT 1 FROM account_follows AS f2 WHERE f2.uid = af.follow_uid AND f2.follow_uid = #{uid}) AS isFollowingMe, " +
            "EXISTS (SELECT 1 FROM account_follows AS f3 WHERE f3.uid = #{uid} AND f3.follow_uid = af.follow_uid) AS isFollowingHim " +
            "FROM " +
            "account_follows AS af " +
            "INNER JOIN account AS a ON af.follow_uid = a.uid " +
            "LEFT JOIN account_info AS ai ON af.follow_uid = ai.uid " +
            "LEFT JOIN account_follows AS f ON af.follow_uid = f.follow_uid " +
            "WHERE af.uid = #{uid} " +
            "GROUP BY af.follow_uid")
    Page<AccountInfoDetails> getFollowingUserInfoDetails(@Param("uid") Long uid);

    @Select("SELECT " +
            "a.uid AS uid, " +
            "ai.head_pic_url AS headPicUrl, " +
            "ai.birthday AS birthday, " +
            "ai.brief_introduction AS briefIntroduction, " +
            "ai.signature AS signature, " +
            "ai.sex AS sex, " +
            "ai.province AS province, " +
            "ai.county AS county, " +
            "a.nickname AS nickname, " +
            "COUNT(f.uid) AS followsCount, " +
            "EXISTS (SELECT 1 FROM account_follows AS f2 WHERE f2.uid = #{uid} AND f2.follow_uid = af.uid) AS isFollowingHim " +
            "FROM " +
            "account_follows AS af " +
            "INNER JOIN account AS a ON af.uid = a.uid " +
            "LEFT JOIN account_info AS ai ON af.uid = ai.uid " +
            "LEFT JOIN account_follows AS f ON af.uid = f.follow_uid " +
            "WHERE af.follow_uid = #{uid} " +
            "GROUP BY af.uid")
    Page<AccountInfoDetails> getFollowerUserInfoDetails(@Param("uid") Long uid);

}
